package com.geeksaga.dao.ibatis.oracle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.memory.UserAttribute;
import org.springframework.stereotype.Repository;

import com.geeksaga.crypt.TripleDes;
import com.geeksaga.dao.UserDao;
import com.geeksaga.dao.ibatis.BaseIBatisDao;
import com.geeksaga.domain.User;
import com.geeksaga.security.UserGrantedAuthority;

/**
 * @author newbie
 * @since 0.1
 */
@Repository
public class UserDaoImpl extends BaseIBatisDao implements UserDao {
	protected final Log logger = LogFactory.getLog(getClass());

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#add(com.geeksaga.domain.User)
	 */
	public User add(User user) throws DataAccessException {
		// String sql = getMessageSourceAccessor().getMessage("user.add");
		//
		// Object[] obj = new Object[] { user.getSid(),
		// user.getPid(),
		// user.getUserId(),
		// user.getPasswd(),
		// user.getEmail(),
		// user.getUserName(),
		// user.getNickName(),
		// user.getImgFile(),
		// user.getIconFile(),
		// StringConvertor.convertorBooleanToString(user.isEnabled()),
		// StringConvertor.convertorBooleanToString(user.isAccountNonExpired()),
		// StringConvertor.convertorBooleanToString(user.isCredentialsNonExpired()),
		// StringConvertor.convertorBooleanToString(user.isAccountNonLocked())};
		//        
		// getJdbcTemplate().update(sql, obj);

		return user;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#find(com.geeksaga.domain.User)
	 */
	public User find(User user) throws DataAccessException {
		// String sql = getMessageSourceAccessor().getMessage("user.find");

		return null;// (User) getJdbcTemplate().queryForObject(sql, new Object[] { user.getPid(), user.getUserId() },
					// getRowMapper());
	}

	// @Autowired
	// private SqlMapClient sqlMap;
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#selectList()
	 */
	@SuppressWarnings("unchecked")
	public List<User> selectList() throws DataAccessException {
		// String sql = getMessageSourceAccessor().getMessage("user.findList");

		// (List<User>)session.selectList("USER.findList", null);
		return (List<User>) getSqlMapClientTemplate().queryForList("USER.selectList");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#findList(com.geeksaga.domain.User)
	 */
	public List<User> findList(User user) throws DataAccessException {
		// String sql = getMessageSourceAccessor().getMessage("user.findFriendsList");

		return null;// (List<User>) getJdbcTemplate().query(sql, new Object[] { user.getSid() }, getRowMapper());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#count()
	 */
	public int count() throws DataAccessException {
		// String sql = getMessageSourceAccessor().getMessage("user.count");
		return (Integer) getSqlMapClientTemplate().queryForObject("USER.count");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#modify(com.geeksaga.domain.User)
	 */
	public int modify(User user) throws DataAccessException {
		// String sql = getMessageSourceAccessor().getMessage("user.modify");
		//        
		// Object[] obj = new Object[] { user.getPasswd(),
		// user.getEmail(),
		// user.getUserName(),
		// user.getNickName(),
		// user.getImgFile(),
		// user.getIconFile(),
		// user.getSid()};
		//  
		// getJdbcTemplate().update(sql, obj);

		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#remove(com.geeksaga.domain.User)
	 */
	public User remove(User user) throws DataAccessException {
		// String sql = getMessageSourceAccessor().getMessage("user.remove");
		//        
		// getJdbcTemplate().update(sql, new Object[] { user.getSid() });

		return user;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#authenticate(com.geeksaga.domain.User)
	 */
	@SuppressWarnings("unchecked")
	public User authenticate(User user) throws DataAccessException, Exception {
		Map<String, String> param = new HashMap<String, String>();
		param.put("pid", user.getPid());
		param.put("userId", user.getUserId());

		List<User> list = (List<User>) getSqlMapClientTemplate().queryForList("USER.authenticate", param);
		User authenrityUser = null;
		UserAttribute userAttributes = new UserAttribute();

		for (User tempoprayUser : list) {
		    tempoprayUser.setPasswd(new TripleDes().decrypt(tempoprayUser.getPasswd()));
		    
			authenrityUser = tempoprayUser;

			UserGrantedAuthority authority = new UserGrantedAuthority(tempoprayUser.getAuthority());

			if (userAttributes.getAuthorities().contains(authority)) {
				int index = userAttributes.getAuthorities().indexOf(authority);
				authority = (UserGrantedAuthority) userAttributes.getAuthorities().get(index);
			} else {
				userAttributes.addAuthority(authority);
			}

			authenrityUser.setAuthorities(userAttributes.getAuthorities());
		}

		return authenrityUser;
	}
}